﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using A_ReferenceCodes.Services.Admin.RefCodes;
using DataModel;
using System.IO;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace A_ReferenceCodes
{
    public class DM_RefCodeBase:DataModel.DataModelBase
    {
        public RefCode RC { get; private set; }
        private int CodeType;
        public DM_RefCodeBase(int CodeType)
        {
            //New Code
            RC = new RefCode();
            this.CodeType = CodeType;
        }
        public DM_RefCodeBase(RefCode RC)
        {
            this.RC = RC;
        }

        public string ShortName
        {
            get { return RC.ShortName; }
            set
            {
                if (RC.ShortName != value)
                {
                    SetComitAction("ShortName", value, UpdateProperty);
                    SetRevertValue("ShortName", RC.Description);
                    RC.ShortName = value;
                    OnPropertyChanged("ShortName");
                }
            }
        }

        public string Description
        {
            get { return RC.Description; }
            set
            {
                if (RC.Description != value)
                {
                    SetComitAction("Description", value, UpdateProperty);
                    SetRevertValue("Description", RC.Description);
                    RC.Description = value;
                    OnPropertyChanged("Description");
                }
            }
        }

        public int Weight
        {
            get { return RC.Weight; }
            set
            {
                if (RC.Weight != value)
                {
                    SetComitAction("Weight", value, UpdateProperty);
                    SetRevertValue("Weight", RC.Weight);
                    RC.Weight = value;
                    OnPropertyChanged("Weight");
                }
            }
        }

        void UpdateProperty(DataModelBase RefC, string PropertyName, object value)
        {
            int ID = ((DM_RefCodeBase)RefC).RC.ID;
            RCC.UpdateRefCodeAsync(ID, PropertyName, value);
        }

        //public string MetaData { get; set; }

        public byte[] MetaDataValue
        {
            get { return RC.MetaData; }
            set
            {
                if (RC.MetaData != value)
                {
                    RC.MetaData = value;
                    SetComitAction("MetaData", value, UpdateProperty);
                    OnPropertyChanged("MetaDataValue");
                    OnPropertyChanged("MetaData");
                }
            }
        }

        #region New
        protected override void ComitNewObject()
        {
            RCC.AddRefCodeCompleted += new EventHandler<AddRefCodeCompletedEventArgs>(RCC_AddRefCodeCompleted);
            RCC.AddRefCodeAsync(CodeType, RC);
        }

        void RCC_AddRefCodeCompleted(object sender, AddRefCodeCompletedEventArgs e)
        {
            RC.ID = e.Result;
        }
        #endregion



        RefCodesServiceClient RCC = ServiceConfig.NewRefCodesService();
    }
    public class DM_RefCode<T>:DM_RefCodeBase
    {
        public DM_RefCode(int CodeType)
            : base(CodeType)
        {
        }
        public DM_RefCode(RefCode RC)
            :base(RC)
        {
        }
        private T GetMetaDataValue()
        {
            MemoryStream MS = new MemoryStream(base.MetaDataValue);
            DataContractSerializer S = new DataContractSerializer(typeof(T));
            return (T)S.ReadObject(MS);
        }
        private void SetMetaDataValue(T value)
        {
            MemoryStream MS = new MemoryStream();
            DataContractSerializer S = new DataContractSerializer(typeof(T));
            S.WriteObject(MS, value);
            base.MetaDataValue = MS.ToArray();
        }
        public T MetaData
        {
            get
            {
                try
                {
                    return GetMetaDataValue(); 
                }
                catch (Exception)
                {
                    return default(T);
                }
                
            }
            set
            { SetMetaDataValue(value); }
        }
    }
}
